package com.hope;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class PillInventory extends ListFragment {
	private EfficientAdapter adapter;
    static String[][] temp_array;
	
	public PillInventory() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.pill_inventory, container, false);
        getActivity().setTitle("Pill Inventory");

        String[][] temp_temp_array = {{"Paracetamol", "(20)", "M, W, F"},{"Clonidine", "(5)", "T, Th"}};
        temp_array = temp_temp_array;
        adapter = new EfficientAdapter(getActivity().getApplicationContext(), this);
        setListAdapter(adapter);
        
        return rootView;
    }
    
    private static class EfficientAdapter extends BaseAdapter {
    	private Context context;
    	private Fragment fragment;
        
        public EfficientAdapter(Context con, Fragment frag) {
        	context = con;
        	fragment = frag;
            LayoutInflater.from(context);
        }

        public int getCount() {
            return temp_array.length;
        }

        public Object getItem(int position) {
            return super.getItemViewType(position);
        }

        public long getItemId(int position) {
            return position;
        }
        
        public String getItemName(int position) {
        	return temp_array[position][0];
        }
        
        public String getItemCount(int position) {
        	return temp_array[position][1];
        }
        
        public String getItemDays(int position) {
        	return temp_array[position][2];
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (convertView == null) {
            	LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            	convertView = inflater.inflate(R.layout.pill_inventory_list_item, parent, false);
                
                holder = new ViewHolder();
                holder.name = (TextView) convertView.findViewById(R.id.pill_inventory_name);
                holder.count = (TextView) convertView.findViewById(R.id.pill_count);
                holder.days = (TextView) convertView.findViewById(R.id.pill_days);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.name.setText(getItemName(position));
            holder.count.setText(getItemCount(position));
            holder.days.setText(getItemDays(position));
            convertView.setOnClickListener(new OnItemClickListener(position));
            return convertView;
        }

        static class ViewHolder {
            TextView name;
            TextView count;
            TextView days;
        }
        
        private class OnItemClickListener implements OnClickListener{           
            //private int mPosition;
            OnItemClickListener(int position) {
            	//mPosition = position;
            }
            
            public void onClick(View view) {
            	ViewHolder holder = (ViewHolder) view.getTag();
            	PillInformation pillFragment = new PillInformation();
                Bundle args = new Bundle();
                args.putString(pillFragment.getArgObject(), (String)holder.name.getText());
                pillFragment.setArguments(args);
            	FragmentTransaction transaction = fragment.getFragmentManager().beginTransaction();

            	// Replace whatever is in the fragment_container view with this fragment,
            	// and add the transaction to the back stack which allows use of back button
            	transaction.replace(R.id.content_frame, pillFragment);
            	transaction.addToBackStack(null);

            	transaction.commit();
            }	
        }
    }
}